package com.jifujun.fastdfs_springboot.controller;


import com.jifujun.fastdfs_springboot.model.FastDFS;
import com.jifujun.fastdfs_springboot.service.FastDFSService;
import com.jifujun.fastdfs_springboot.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * Create with IntelliJ IDEA
 * Project Name: fast_dfs
 * Package Name: com.jifujun.fast_dfs.controller
 * Date: 2018/3/2713:54
 * User: 快乐的小菌菌
 * Description:
 */
@Api("FastDFS 相关接口")
@CrossOrigin
@RestController
@RequestMapping("/FastDFSController")
public class FastDFSController {
    private FastDFSService fastDFSService;

    public FastDFSController(FastDFSService fastDFSService) {
        this.fastDFSService = fastDFSService;
    }

    @PostMapping
    @ApiOperation("文件列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页", name = "pageNo", required = true, dataType = "int", paramType = "list"),
            @ApiImplicitParam(value = "页数", name = "pageSize", required = true, dataType = "int", paramType = "list"),
    })
    public Result list(
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam("pageSize") Integer pageSize
    ) {
        fastDFSService.queryList(pageNo, pageSize);
        return Result.ok();
    }

    @PostMapping("/upload")
    @ApiOperation("上传本地文件")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "上传文件的路径", name = "path", required = true, dataType = "string", paramType = "query"),
    })
    public Result upload(
            @RequestParam("path") String path
    ) {
        String filePath = fastDFSService.upload(path);
        return Result.ok(filePath);
    }

    @PostMapping("/uploadVue")
    @ApiOperation("上传本地文件")
    public Result uploadVue(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String[] fileDescs = originalFilename.split("\\.");
        String filePath = fastDFSService.upload(file.getBytes(), fileDescs[1], fileDescs[0]);
        return Result.ok(filePath);
    }

    @PostMapping("/uploadRemoteLink")
    @ApiOperation("上传远程文件")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "上传文件的url", name = "remoteLink", required = true, dataType = "string", paramType = "query"),
    })
    public Result uploadRemoteLink(
            @RequestParam("remoteLink") String remoteLink
    ) {
        String filePath = fastDFSService.uploadRemoteLink(remoteLink);
        return Result.ok(filePath);
    }

    @PostMapping("/download")
    @ApiOperation("下载文件")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "文件id", name = "id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(value = "下载到本地的路径", name = "localPath", required = true, dataType = "string", paramType = "query")
    })
    public Result download(
            @RequestParam("id") Integer id,
            @RequestParam("localPath") String localPath
    ) {
        fastDFSService.download(id, localPath);
        return Result.ok();
    }

    @ApiOperation("删除文件(id)")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "文件id", name = "id", required = true, dataType = "int", paramType = "query"),
    })
    @PostMapping("/deleteById")
    public Result deleteById(
            @RequestParam("id") Integer id
    ) {
        fastDFSService.deleteById(id);
        return Result.ok();
    }

    @ApiOperation("删除文件(路径)")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "文件路径 包括group", name = "path", required = true, dataType = "string", paramType = "query"),
    })
    @PostMapping("/deleteByPath")
    public Result deleteByPath(
            @RequestParam("path") String path
    ) {
        Integer code = fastDFSService.deleteByPath(path);
        return Result.ok(code);
    }

    @ApiOperation("获得一个文件的详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "文件id", name = "id", required = true, dataType = "int", paramType = "query"),
    })
    @PostMapping("/get")
    public Result get(
            @RequestParam("id") Integer id
    ) {
        FastDFS fastDFS = fastDFSService.getById(id);
        return Result.ok(fastDFS.toString());
    }

    @ApiOperation("获得一个文件的路径")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "文件id", name = "id", required = true, dataType = "int", paramType = "query"),
    })
    @PostMapping("/getPath")
    public Result getPath(
            @RequestParam("id") Integer id
    ) {
        FastDFS fastDFS = fastDFSService.getById(id);
        String filePath = fastDFS.getGroup_name() + File.separatorChar + fastDFS.getStorage_path();
        return Result.ok(filePath);
    }
}
